package com.gujiangbo.threadpool;

/**
 * 拒绝策略，当任务超过最大值时该触发哪种策略通知提交者
 */
public interface DenyPolicy {

    void reject(Runnable runnable, ThreadPool pool);

    /**
     * 直接丢弃
     */
    class DiscardDenyPolicy implements DenyPolicy {

        @Override
        public void reject(Runnable runnable, ThreadPool pool) {
            //do nothing.....
        }
    }

    /**
     * 抛出异常
     */
    class AbortDenyPolicy implements DenyPolicy {

        @Override
        public void reject(Runnable runnable, ThreadPool pool) {
            throw new RunnableDenyException("the runnable " + runnable + " will be abort.");
        }
    }

    /**
     * 是任务在提交者所在的线程中执行任务
     */
    class RunnerDenyPolicy implements DenyPolicy {

        @Override
        public void reject(Runnable runnable, ThreadPool pool) {
            if (!pool.isShutdown()) {
                runnable.run();
            }
        }
    }

}
